import Vue from "vue";
import App from "./App.vue";
import router from "@/router"; // 路由对象
import "@/style/common.css"; // 导入公共样式
import { db } from "@/db"; // 导入数据对象

// key of local storage
const LOCAL_MONEY = "dona-sadqioklasdhlhasdla";
const LOCAL_ORG = "org-asdfhashdafl";
Vue.config.productionTip = false;

Vue.prototype.$db = db;

//初始化捐款数组数据
function initData() {
    // a.初始化单位数组数据
    db.listOrgs.push({ id: -1, name: "请选择" });
    db.listOrgs.push({ id: 1, name: "壹基金" });
    db.listOrgs.push({ id: 2, name: "慈济基金" });
    db.listOrgs.push({ id: 3, name: "宋庆龄基金" });
    localStorage.setItem(LOCAL_ORG, JSON.stringify(db.listOrgs));
    // b.向捐款数组 添加 初始数据，可以用 add 或 addObj 来添加
    db.listMoney.add("李连杰", "1", "10000", "2023-01-17");
    db.listMoney.add("成龙", "2", "20000", "2023-01-18");
    db.listMoney.addObj({
        pname: "甄子丹",
        orgId: "3",
        money: "40000",
        date: "2023-01-19",
    });
    db.listMoney.addObj({
        pname: "洪金宝",
        orgId: "3",
        money: "140000",
        date: "2023-01-19",
    });
    db.listMoney.add("徐静蕾", "2", "2220000", "2023-01-17");
    db.listMoney.add("陈光标", "2", "1000", "2023-01-18");
    db.listMoney.add("星爷", "1", "10000", "2023-01-17");
    db.listMoney.add("梁朝伟", "2", "20000", "2023-01-18");
    db.listMoney.add("周润发", "1", "10000", "2023-01-17");
    db.listMoney.add("马云", "3", "2000000", "2023-01-18");
    db.listMoney.add("马云他儿子", "3", "2000000", "2023-01-18"); //11 行
}

// get local data
const dataMoney = JSON.parse(localStorage.getItem(LOCAL_MONEY));
const dataOrg = JSON.parse(localStorage.getItem(LOCAL_ORG));

// no local data use initial data
if (!dataMoney || !dataOrg) initData();
else {
    dataOrg.forEach(ele => db.listOrgs.push(ele));
    dataMoney.forEach(ele => db.listMoney.push(ele));
    console.log(dataMoney);
}
import BaseTable from "@/components/BaseTable";
import BaseSelect from "@/components/BaseSelect";

// register base components : table and select
Vue.component("BaseTable", BaseTable);
Vue.component("BaseSelect", BaseSelect);

//  hang some methods on Vue prototype for global usage
Vue.prototype.isCompleteData = function() {
    return (
        this.pname &&
        !isNaN(this.money) &&
        this.isFormatDate(this.date) &&
        this.selectOption > 0
    );
};

//param {date} must be a string like this:'xxxx-x-x'
Vue.prototype.formatDate = function(date) {
    // return  'xxxx-xx-xx'
    let dateYear = date.split("-")[0];
    let dateMonth = date.split("-")[1];
    let dateDate = date.split("-")[2];
    dateMonth = dateMonth >= 10 ? dateMonth : `0${dateMonth}`;
    dateDate = dateDate >= 10 ? dateDate : `0${dateDate}`;
    return `${dateYear}-${dateMonth}-${dateDate}`;
};

// param {year} must be in range(1000,9999)
function isLeapYear(year) {
    return (!(year % 4) && year % 100) || !(year % 400);
}

// param {year,month,date}
function isMonthDateCmp(year, month, date) {
    //return whether year-month-date is regular
    if (month == 2) return date <= 28 + isLeapYear(year);
    if (
        month == 1 ||
        month == 3 ||
        month == 5 ||
        month == 7 ||
        month == 8 ||
        month == 10 ||
        month == 12
    ) {
        return date <= 31;
    } else return date <= 30;
}

// param {date} user input date string
Vue.prototype.isFormatDate = function(date) {
    // return whether date string like 'xxxx-x-x' and year-month-date is regular
    return (
        /^\d{4}-\d{1,2}-\d{1,2}$/.test(date) &&
        date.split("-")[1] <= 12 &&
        isMonthDateCmp(date.split("-")[0], date.split("-")[1], date.split("-")[2])
    );
};

// param {option ,el} get the option value in base component select, el is parent of select component
Vue.prototype.getOption = function(option, el) {
    if(el) el.selectOption = +option;
};
new Vue({
    router,
    render: h => h(App),
}).$mount("#app");